package components {
    import beans.RoomBean;
    import common.Role;
    import events.RoomClickEvent;
    import skins.BookedRoomSkin;
    import skins.RequestRoomSkin;
    import skins.RoomSkin;
    
    import flash.events.MouseEvent;
    
    public class Room extends MultilineButton {
    	
    	//------------------------------------------------------------
        //
        // class constants
        //
        //------------------------------------------------------------
        
        /**
         * default width
         */
        public static const STANDARD_WIDTH:int = 64;
        
        /**
         * default heigth
         */
        public static const STANDARD_HEIGHT:int = 46;
        
        //------------------------------
        // room status
        //------------------------------

        /**
         * unbooked status
         */
        public static const UNBOOKED:String = "unbooked";
        
        /**
         * booked status
         */
        public static const BOOKED:String = "booked";
        
        /**
         * unapproved status
         */
        public static const UNAPPROVED:String = "unapproved";
        
        /**
         * invalid status
         */
        public static const INVALID:String = "invalid";
        
        //------------------------------------------------------------
        //
        // variables
        //
        //------------------------------------------------------------
        
        //------------------------------
        // dataProvider
        //------------------------------

        /**
         * data provider of room
         */
        private var _dataProvider:RoomBean;

        public function get dataProvider():RoomBean {
            return _dataProvider;
        }

        public function set dataProvider(dataProvider:RoomBean):void {
            if (!dataProvider) {
            	return;
            }
            
            _dataProvider = dataProvider;
            
            // set status and content of the room
            if (dataProvider.companyID != 0) {
                status = BOOKED;
                content = dataProvider.companyName;
                return;
            }
            
            if (dataProvider.companyID == 0 && dataProvider.requestCount == 0) {
            	status = UNBOOKED;
            	return;
            }
            
            if (dataProvider.requestCount > 0 && (role != Role.VISITOR) ) {
            	status = UNAPPROVED;
            	if (userRole != Role.VISITOR) {
            		content = roomNo + "\n(" + dataProvider.requestCount + ")";
            	}
            	return;
            } else {
            	status = UNBOOKED;
            	content = roomNo;
            	return;
            }
        }
        
        //------------------------------
	    // role
	    //------------------------------
	
	    /**
	     * the role of current user
	     */
	    private function get role():String {
	        return parentApplication.role;
	    }
        
        //------------------------------
        // status
        //------------------------------

        /**
         * room status
         */
        private var _status:String;

        public function get status():String {
            return _status;
        }

        public function set status(status:String):void {
            _status = status;
            
            switch (status) {
            	case UNBOOKED: {
            		setStyle("skin", RoomSkin);
            		setStyle("fontSize", 10);
            		setStyle("fontWeight", "normal");
            		toolTip = null;
            		break;
            	}
            	
            	case BOOKED: {
            		if (userRole == Role.COMPANY || userRole == Role.ADMIN) {
	            		setStyle("skin", BookedRoomSkin);
            		} else {
            			setStyle("skin", RoomSkin);
            		}
            		
            		setStyle("fontSize", 10);
                    setStyle("fontWeight", "bold");
                    
                    toolTip = "展位编号 - " + Room(parentApplication.rooms[dataProvider.roomID]).roomNo;
	                toolTip += "\n公司名称 - " + dataProvider.companyName;
	                
                    break;
                }
                
                case UNAPPROVED: {
            		if (userRole == Role.COMPANY || userRole == Role.ADMIN) {
	            		setStyle("fontWeight", "bold");
	            		setStyle("fontSize", 10);
	            		setStyle("skin", RequestRoomSkin);
	            		toolTip = null;
	            		break;
            		}
            		
                	setStyle("fontSize", 10);
                    setStyle("fontWeight", "normal");
                    toolTip = null;
                    break;
                }
                
                case INVALID: {
                	setStyle("fontSize", 18);
	                setStyle("fontWeight", "normal");
	                setStyle("color", 0x888888);
                    break;
                }
            }
            
            setOperationStatus();
        }
        
        //------------------------------
        // borderSize
        //------------------------------

        /**
         * border thickness of the room
         */
        private static var _borderSize:int;

        public function get borderSize():int {
            if (_borderSize == 0) {
            	_borderSize = getStyle("borderThickness");
            }
            return _borderSize;
        }
        
        //------------------------------
        // roomID
        //------------------------------

        /**
         * room identity, used for application, different with room NO.
         */
        private var _roomID:int;

        public function get roomID():int {
            return _roomID;
        }

        public function set roomID(roomID:int):void {
            _roomID = roomID;
        }
        
        //------------------------------
        // roomNo
        //------------------------------

        /**
         * room no, used for user, administrator will assign this value
         */
        private var _roomNo:String;

        public function get roomNo():String {
            return _roomNo;
        }

        public function set roomNo(roomNo:String):void {
            _roomNo = roomNo;
        }
        
        //------------------------------
        // content
        //------------------------------

        /**
         * the text content of the room
         */
        public function get content():String {
            return label;
        }

        public function set content(content:String):void {
            label = content;
        }
        
        //------------------------------
        // userRole
        //------------------------------

        /**
         * the role of current user
         */
        private function get userRole():String {
            return parentApplication.role;
        }
        
        //------------------------------------------------------------
        //
        // constructor
        //
        //------------------------------------------------------------
        
        public function Room() {
        	super();
        	
            width = STANDARD_WIDTH;
            height = STANDARD_HEIGHT;
            
            addEventListener(MouseEvent.CLICK, onClick);
        }
        
        //------------------------------------------------------------
        //
        // methods
        //
        //------------------------------------------------------------
        
        private function setOperationStatus():void {
        	if (userRole == Role.VISITOR) {
        		if (status == BOOKED) {
        			buttonMode = true;
        		} else {
        			buttonMode = false;
        		}
        	}
        	
        	if (userRole == Role.COMPANY) {
        		if (status == BOOKED) {
        			buttonMode = false;
        		} else {
        			buttonMode = true;
        		}
        	}
        	
        	if (userRole == Role.ADMIN) {
         		if (status == BOOKED) {
        			buttonMode = false;
        		} else {
        			buttonMode = true;
        		}
        	}
        }
        
        //------------------------------------------------------------
        //
        // event handlers
        //
        //------------------------------------------------------------
        
        private function onClick(e:MouseEvent):void {
        	if (buttonMode) {
        		dispatchEvent(new RoomClickEvent(roomID));
        	}
        }
    }
}

